﻿@model IEnumerable<EmgrMvc.Models.Device>

@{
    ViewData["Title"] = "设备列表";
}

<script>
    function deviceOn(item) {
        var id = $(item).data("id");
        $.post("/api/app/device/on/" + id, (resp, status) => {
            if (status == "success") {
                alert("设备已打开");
            }
        })
    }

    function deviceOff(item) {
        var id = $(item).data("id");
        $.post("/api/app/device/off/" + id, (resp, status) => {
            if (status == "success") {
                alert("设备已关闭");
            }
        })
    }
</script>

<h2>设备列表</h2>

<p>
    <a asp-action="Create">添加设备</a>
</p>
<p>
    <a asp-action="AddIRDevice">添加红外遥控设备</a>
</p>
<p>
    <a asp-action="CreateWOLDevice">添加远程唤醒设备</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Description)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Type)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ControllerNode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Topic)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Data)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Status)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Latest)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Type)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ControllerNode)
            </td>
            <th>
                @Html.DisplayFor(modelItem => item.Topic)
            </th>
            <td>
                @Html.DisplayFor(modelItem => item.Data)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Status)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Latest)
            </td>
            <td>
                <a onclick="deviceOn(this)" data-id="@item.Id">开机</a> |
                <a onclick="deviceOff(this)" data-id="@item.Id">关机</a> |
                <a asp-action="Details" asp-route-id="@item.Id">查看</a> |
                <a asp-action="Delete" asp-route-id="@item.Id">删除</a>
            </td>
        </tr>
}
    </tbody>
</table>
